package com.gzmpc.kafka.produce;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.gzmpc.kafka.entity.User;
import com.gzmpc.kafka.util.DateUtil;
import com.gzmpc.kafka.util.NumberUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author:lizhicheng
 * @email:lizhicheng@gzmpc.com
 * @date:2024-03-22 15:58:08
 */
@RestController
@Slf4j
public class KafkaProducer {
    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Scheduled(fixedDelay = 1000 * 1)
    public void sendMessage() throws ParseException {
        User user = new User();
        user.setId(IdUtil.simpleUUID());
        user.setUsername(IdUtil.simpleUUID().substring(0,10));
        user.setPassword(IdUtil.simpleUUID().substring(0,10));
        user.setAddress(IdUtil.simpleUUID().substring(0,10));
        user.setEmail(IdUtil.simpleUUID().substring(0,10));
        user.setProfile(IdUtil.simpleUUID().substring(0,10));
        user.setBirthday(DateUtil.randomDate());
        user.setRegisterDay(DateUtil.randomDate());
        user.setLoginDay(DateUtil.randomDate());
        user.setStatus(ThreadLocalRandom.current().nextInt(0,2));
        user.setAccount(NumberUtil.generateRandomBigDecimal(0,100));
        user.setBalance(NumberUtil.generateRandomBigDecimal(0,200));
        user.setAge(ThreadLocalRandom.current().nextInt(0,100));
        user.setSex(ThreadLocalRandom.current().nextInt(0,2));
        user.setAvatar(IdUtil.simpleUUID().substring(0,10));
        user.setLevel(ThreadLocalRandom.current().nextInt(0,100));
        //发送消息
        kafkaTemplate.send("test", JSON.toJSONString(user));
        log.info("kafka producer send message : {}", JSON.toJSONString(user));

    }

}
